package a08_回溯算法;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * a02_组合复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/2/9
 */
public class a02_组合复习2 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        traversal(result, path, n, k, 1);
        return result;
    }

    public void traversal(List<List<Integer>> result,
                          LinkedList<Integer> path,
                          int n,
                          int k,
                          int startIdx){
        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIdx; i <= n; i++) {
            path.add(i);
            traversal(result, path, n, k, i + 1);
            path.removeLast();
        }

    }
}
